package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import java.util.Objects;

import com.iwhalecloud.citybrain.flow.platform.manager.application.service.CommandHandler;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.NodeCandidateService;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpCommandResultDto;
import com.iwhalecloud.citybrain.flow.platform.manager.common.client.HttpPageableQueryDto;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.monitor.FlowTaskTraceVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.task.CommentVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowTaskTraceRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.IFlowableCommentService;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author lilh
 * @date 2019-02-19 22:54
 */
public abstract class AbstractCommandHandler implements CommandHandler {

    protected Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected RuntimeService runtimeService;

    @Autowired
    protected TaskService taskService;

    @Autowired
    protected RepositoryService repositoryService;

    @Autowired
    protected FlowTaskTraceRepository flowTaskTraceRepository;

    @Autowired
    private IFlowableCommentService flowableCommentService;

    @Autowired
    private NodeCandidateService nodeCandidateService;

    protected HttpCommandResultDto resolveResult(ProcessInstance processInstance, String errMsg, String successMsg) {
        if (Objects.isNull(processInstance)) {
            HttpCommandResultDto resultDto = new HttpCommandResultDto();
            resultDto.setDealFlag(false);
            resultDto.setDealMsg(errMsg);
            return resultDto;
        }

        return resolveResult(processInstance.getProcessInstanceId(), errMsg, successMsg);
    }

    protected HttpCommandResultDto resolveResult(String errMsg, String successMsg) {
        if (Objects.nonNull(errMsg)) {
            return resolveResult((String) null, errMsg, successMsg);
        }
        HttpCommandResultDto result = new HttpCommandResultDto();
        result.setDealFlag(true);
        result.setDealMsg(successMsg);
        return result;
    }

    protected HttpCommandResultDto resolveResult(String processInstanceId, String errMsg, String successMsg) {
        HttpCommandResultDto resultDto = new HttpCommandResultDto();
        if (Objects.isNull(processInstanceId)) {
            resultDto.setDealFlag(false);
            resultDto.setDealMsg(errMsg);
            return resultDto;
        }
        resultDto.setDealMsg(successMsg);
        resultDto.setProcessInstanceId(processInstanceId);
        return resultDto;
    }

    protected ProcessInstance resolveProcessInstance(String processInstanceId) {
        if (StringUtils.isEmpty(processInstanceId)) {
            logger.warn("未传递流程实例id[processInstanceId]");
            return null;
        }

        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).active().singleResult();
        if (Objects.isNull(processInstance)) {
            logger.warn("流程实例 {} 不存在或者已挂起", processInstanceId);
            return null;
        }

        return processInstance;
    }

    protected HttpCommandResultDto resolveFailResult(String processInstanceId, String errorMsg) {
        HttpCommandResultDto resultDto = new HttpCommandResultDto();
        resultDto.setDealFlag(false);
        resultDto.setDealMsg(errorMsg);
        resultDto.setProcessInstanceId(processInstanceId);
        return resultDto;
    }

    protected void processPageParams(HttpPageableQueryDto taskListQueryDto) {
        if (Objects.isNull(taskListQueryDto.getPageNum()) || taskListQueryDto.getPageNum() <= 0) {
            taskListQueryDto.setPageNum(1);
        }

        if (Objects.isNull(taskListQueryDto.getPageSize()) || taskListQueryDto.getPageSize() <= 0) {
            taskListQueryDto.setPageSize(10);
        }

        //单次最多查询100条
        if (taskListQueryDto.getPageSize() > 100) {
            taskListQueryDto.setPageSize(100);
        }
    }

    protected void addComment(String taskId, String userCode, String processInstanceId, String type, String message) {
        //1.添加备注
        CommentVo commentVo = new CommentVo(taskId, userCode, processInstanceId, type, message);
        flowableCommentService.addComment(commentVo);
    }

    protected boolean checkIsRequiredComment(String taskId){
        boolean need = false;
        FlowTaskTraceVo flowTaskTraceVo = flowTaskTraceRepository.getTaskTraceByTaskId(taskId);
        if (Objects.nonNull(flowTaskTraceVo)){
            String processInstanceId = flowTaskTraceVo.getProcessInstanceId();
            need = nodeCandidateService.isRequiredComment(processInstanceId,flowTaskTraceVo.getActivityId());
        }
        return need;
    }

    /**
     * 检测某个意见是否必须
     * @param taskId
     * @return
     */
    protected boolean checkIsRequired(String taskId, String idea){
        boolean need = false;
        FlowTaskTraceVo flowTaskTraceVo = flowTaskTraceRepository.getTaskTraceByTaskId(taskId);
        if (Objects.nonNull(flowTaskTraceVo)){
            String processInstanceId = flowTaskTraceVo.getProcessInstanceId();
            need = nodeCandidateService.checkIsRequired(processInstanceId,flowTaskTraceVo.getActivityId(),idea);
        }
        return need;
    }
}
